//index.js
import http from "../../../class/Utils/Http"
import tips from "../../../class/Utils/Tips"
import user from "../../../class/Utils/User"
Page({
    data: {
        page: '1',
        list: []
    },
    onLoad: function () {
        var that = this
        var user = wx.getStorageSync('user')
        var data = {
            uid: user.userid
        }
        http.req("/Api/sc_list", data, function (res) {
            console.log(res)
            if (res.pw_rec_total == 0) {
                that.setData({
                    nodata: true
                })
            }else{
                for (var i = 0; i < res.pw_rec_list.length; i++) {
                    res.pw_rec_list[i].changduans = 0;
                    res.pw_rec_list[i].liuyan_state = 0;   // 留言是否展开
                    res.pw_rec_list[i].liuyan_gengduo = 0; //留言是否大于5条
                    res.pw_rec_list[i].liuyan_list  = [];  //已留言内容
                    res.pw_rec_list[i].liuyan_content = ''; //正在留言内容
                    res.pw_rec_list[i].liuyan_disabled = false; //正在按钮状态
                }
                that.setData({
                    list: res.pw_rec_list
                })
            }
        })
    },
    //打开留言
    liuyanbtn: function (e) {
        var that = this
        var mid = e.currentTarget.dataset.id
        var index = e.currentTarget.dataset.index
        var list = that.data.list
        user.log_user(that, function () {
            list[index].liuyan_state = !list[index].liuyan_state
            that.setData({
                list: list
            })
            /*获取留言内容*/
            if(list[index].liuyan_state){
                var data = {
                    uid: wx.getStorageSync('user').userid,
                    mid: mid
                }
                http.reqload("/Api/liuyan_list", data, function (res) {
                    // console.log(res)
                    list[index].liuyan_list = res
                    if(list[index].liuyan_list.length >5){
                        list[index].liuyan_list.splice(5)
                        list[index].liuyan_gengduo = 1
                    }
                    that.setData({
                        list: list
                    })
                })
            }
            
        })

    },
    /*
     输入内容
     */
    content:function (e) {
        var that = this
        var index = e.currentTarget.dataset.index
        var list = that.data.list
        list[index].liuyan_content = e.detail.value
        this.setData({
            list:list
        })
    },
    liuyan:function (e) {
        var that = this
        var user = wx.getStorageSync("user")
        var mid = e.currentTarget.dataset.id
        var index = e.currentTarget.dataset.index
        var list = that.data.list
        if(list[index].liuyan_content==''){
            tips.error('请输入留言内容')
            return
        }
        list[index].liuyan_disabled = true
        that.setData({
            list:list
        })
        var data = {
            uid:user.userid,
            content:list[index].liuyan_content,
            mid:mid
        }


        http.reqload("/Api/add_liuyan",data,function (res) {
            if(res.code=='202'){
                console.log(res);
                tips.ok(res.msg_val)
                list[index].liuyan_list.unshift({'poster':res.poster,'content':res.content})
                list[index].liuyan_disabled = false
                list[index].liuyan_content = ''
                if(list[index].liuyan_list.length>5){
                    list[index].liuyan_list.splice(5)
                    list[index].liuyan_gengduo = 1
                }
                that.setData({
                    list:list
                })
            }else{
                tips.error(res.msg_val)
            }
        })
        console.log(list)
    },
    //长短 介绍切换
    changeintro: function (e) {
        var that = this
        var presentindex = e.currentTarget.dataset.index
        var datalist = that.data.list
        datalist[presentindex].changduans = !datalist[presentindex].changduans
        that.setData({
            list: datalist
        })
    },
    /*
     点击名片
     */
    mingpian:function (e) {
        var that = this
        user.log_user(this, function (res) {
            var poster = e.currentTarget.dataset.poster
            var cid = e.currentTarget.dataset.cid
            if(cid){
                var dd ={
                    poster:poster,
                    cid:cid
                }
                console.log(dd)
                http.reqload("/Api/mingpian",dd,function (res) {
                    console.log(res)
                    if(res.code == 200){
                        tips.goto('/pages/mingpian/card/card?id='+res.mid)
                    }else{
                        tips.error(res.msg_val)
                    }
                })
            }else{
                tips.error("暂无名片")
            }

        })
    },
    /*
     删除收藏
     */
    del:function (e) {
        var that = this
        var user = wx.getStorageSync("user")
        var id = e.currentTarget.dataset.id
        var index = e.currentTarget.dataset.index
        var list = that.data.list
        tips.modal("是否取消收藏该商机",true,'function',false,function () {
            var data = {
                uid:user.userid,
                id:id
            }

            http.req("/Api/shoucang_del",data,function (res) {
                if (res.code == '208') {
                    tips.ok(res.msg_val)
                    list.splice(index, 1);
                    if (list.length == 0) {
                        that.setData({
                            nodata: true
                        })
                    }
                    that.setData({
                        list
                    })
                } else {
                    tips.error(res.msg_val)
                }
            })
        })
    },
    onReachBottom: function () {
        var that = this
        var page = that.data.page
        page++
        that.setData({
            page: page
        })

        var list = that.data.list
        var user = wx.getStorageSync('user')
        var data = {
            uid: user.userid,
            page: page
        }
        http.reqload("/Api/sc_list", data, function (res) {
            console.log(data);
            for (var i = 0; i < res.pw_rec_list.length; i++) {
                res.pw_rec_list[i].changduans = 0;
                res.pw_rec_list[i].liuyan_state = 0;   // 留言是否展开
                res.pw_rec_list[i].liuyan_gengduo = 0; //留言是否大于5条
                res.pw_rec_list[i].liuyan_list  = [];  //已留言内容
                res.pw_rec_list[i].liuyan_content = ''; //正在留言内容
                res.pw_rec_list[i].liuyan_disabled = false; //正在按钮状态
            }
            that.setData({
                list: res.pw_rec_list
            })
        })
    },
  //点击图片放大
  previewImage: function (e) {
    var that = this;
    console.log(e)


    //获取点击的数据里的图片

    var thisindex = e.currentTarget.dataset.index
    var thatdatalist = that.data.list
    var thatdatalistindex = thatdatalist[thisindex].pic_list_img



    var newimgdata = [];
    for (var i = 0; i < thatdatalistindex.length; i++) {
      if (thatdatalistindex[i]) {
        newimgdata.push('http://img.i.album.toocle.com/0-0-0/' + thatdatalistindex[i]);
      }

    }
    that.setData({
      newimgdata: newimgdata,
    });

    console.log(newimgdata)







    var current = e.currentTarget.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: that.data.newimgdata // 需要预览的图片http链接列表  
    })
  },
})
